home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / TERM.C < prev    next >
C/C++ Source or Header  |  1991-08-26  |  7KB  |  228 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/term.c,v 1.5 1991/08/26 15:00:20 arthur Exp $
  4.  
  5. Copyright (c) 1990 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include "scheme.h"
  36. #include "ostop.h"
  37.  
  38. extern long death_blow;
  39. extern char * Term_Messages [];
  40. extern void EXFUN (get_band_parameters, (long * heap_size, long * const_size));
  41. extern void EXFUN (Reset_Memory, (void));
  42.  
  43. #define BYTES_TO_BLOCKS(n) (((n) + 1023) / 1024)
  44. #define MIN_HEAP_DELTA    50
  45.  
  46. #ifndef EXIT_SCHEME
  47. #define EXIT_SCHEME exit
  48. #endif
  49.  
  50. #ifdef EXIT_SCHEME_DECLARATIONS
  51. EXIT_SCHEME_DECLARATIONS;
  52. #endif
  53.  
  54. void
  55. DEFUN_VOID (init_exit_scheme)
  56. {
  57. #ifdef INIT_EXIT_SCHEME
  58.   INIT_EXIT_SCHEME ();
  59. #endif
  60. }
  61.  
  62. static void
  63. DEFUN (attempt_termination_backout, (code), int code)
  64. {
  65.   fflush (stderr);
  66.   if ((WITHIN_CRITICAL_SECTION_P ())
  67.       || (code == TERM_HALT)
  68.       || (! (Valid_Fixed_Obj_Vector ())))
  69.     return;
  70.   {
  71.     SCHEME_OBJECT Term_Vector = (Get_Fixed_Obj_Slot (Termination_Proc_Vector));
  72.     if ((! (VECTOR_P (Term_Vector)))
  73.     || ((VECTOR_LENGTH (Term_Vector)) <= code))
  74.       return;
  75.     {
  76.       SCHEME_OBJECT Handler = (VECTOR_REF (Term_Vector, code));
  77.       if (Handler == SHARP_F)
  78.     return;
  79.      Will_Push (CONTINUATION_SIZE
  80.         + STACK_ENV_EXTRA_SLOTS
  81.         + ((code == TERM_NO_ERROR_HANDLER) ? 5 : 4));
  82.       Store_Return (RC_HALT);
  83.       Store_Expression (LONG_TO_UNSIGNED_FIXNUM (code));
  84.       Save_Cont ();
  85.       if (code == TERM_NO_ERROR_HANDLER)
  86.     STACK_PUSH (LONG_TO_UNSIGNED_FIXNUM (death_blow));
  87.       STACK_PUSH (Val);            /* Arg 3 */
  88.       STACK_PUSH (Fetch_Env ());    /* Arg 2 */
  89.       STACK_PUSH (Fetch_Expression ()); /* Arg 1 */
  90.       STACK_PUSH (Handler);        /* The handler function */
  91.       STACK_PUSH (STACK_FRAME_HEADER
  92.           + ((code == TERM_NO_ERROR_HANDLER) ? 4 : 3));
  93.      Pushed ();
  94.       abort_to_interpreter (PRIM_NO_TRAP_APPLY);
  95.     }
  96.   }
  97. }
  98.  
  99. static void
  100. DEFUN (termination_prefix, (code), int code)
  101. {
  102.   attempt_termination_backout (code);
  103.   OS_restore_external_state ();
  104.   putc ('\n', stdout);
  105.   if ((code < 0) || (code > MAX_TERMINATION))
  106.     fprintf (stdout, "Unknown termination code 0x%x", code);
  107.   else
  108.     fputs ((Term_Messages [code]), stdout);
  109.   if ((WITHIN_CRITICAL_SECTION_P ()) && (code != TERM_HALT))
  110.     fprintf (stdout, " within critical section \"%s\"",
  111.          (CRITICAL_SECTION_NAME ()));
  112.   fputs (".\n", stdout);
  113. }
  114.  
  115. static void
  116. DEFUN (termination_suffix, (code, value, abnormal_p),
  117.        int code AND int value AND int abnormal_p)
  118. {
  119. #ifdef EXIT_HOOK
  120.   EXIT_HOOK (code, value, abnormal_p);
  121. #endif
  122.   fflush (stdout);
  123.   Reset_Memory ();
  124.   EXIT_SCHEME (value);
  125. }
  126.  
  127. static void
  128. DEFUN (termination_suffix_trace, (code), int code)
  129. {
  130.   if (Trace_On_Error)
  131.     {
  132.       fprintf (stdout, "\n\n**** Stack trace ****\n\n");
  133.       Back_Trace (stdout);
  134.     }
  135.   termination_suffix (code, 1, 1);
  136. }
  137.  
  138. void
  139. DEFUN (Microcode_Termination, (code), int code)
  140. {
  141.   termination_prefix (code);
  142.   termination_suffix_trace (code);
  143. }
  144.  
  145. void
  146. DEFUN (termination_normal, (value), CONST int value)
  147. {
  148.   termination_prefix (TERM_HALT);
  149.   termination_suffix (TERM_HALT, value, 0);
  150. }
  151.  
  152. void
  153. DEFUN_VOID (termination_init_error)
  154. {
  155.   termination_prefix (TERM_EXIT);
  156.   termination_suffix (TERM_EXIT, 1, 1);
  157. }
  158.  
  159. void
  160. DEFUN_VOID (termination_end_of_computation)
  161. {
  162.   termination_prefix (TERM_END_OF_COMPUTATION);
  163.   Print_Expression (Val, "Final result");
  164.   putc ('\n', stdout);
  165.   termination_suffix (TERM_END_OF_COMPUTATION, 0, 0);
  166. }
  167.  
  168. void
  169. DEFUN_VOID (termination_trap)
  170. {
  171.   /* This claims not to be abnormal so that the user will
  172.      not be asked a second time about dumping core. */
  173.   termination_prefix (TERM_TRAP);
  174.   termination_suffix (TERM_TRAP, 1, 0);
  175. }
  176.  
  177. void
  178. DEFUN_VOID (termination_no_error_handler)
  179. {
  180.   /* This does not print a back trace because the caller printed one. */
  181.   termination_prefix (TERM_NO_ERROR_HANDLER);
  182.   if (death_blow == ERR_FASL_FILE_TOO_BIG)
  183.     {
  184.       long heap_size;
  185.       long const_size;
  186.       get_band_parameters (&heap_size, &const_size);
  187.       fputs ("Try again with values at least as large as\n", stdout);
  188.       fprintf (stdout, "  -heap %d (%d + %d)\n",
  189.            (MIN_HEAP_DELTA + (BYTES_TO_BLOCKS (heap_size))),
  190.            (BYTES_TO_BLOCKS (heap_size)),
  191.            MIN_HEAP_DELTA);
  192.       fprintf (stdout, "  -constant %d\n", (BYTES_TO_BLOCKS (const_size)));
  193.     }
  194.   termination_suffix (TERM_NO_ERROR_HANDLER, 1, 1);
  195. }
  196.  
  197. void
  198. DEFUN_VOID (termination_gc_out_of_space)
  199. {
  200.   termination_prefix (TERM_GC_OUT_OF_SPACE);
  201.   fputs ("You are out of space at the end of a Garbage Collection!\n",
  202.      stdout);
  203.   fprintf (stdout, "Free = 0x%lx; MemTop = 0x%lx; Heap_Top = 0x%lx\n",
  204.        Free, MemTop, Heap_Top);
  205.   fprintf (stdout, "Words required = %ld; Words available = %ld\n",
  206.        (MemTop - Free), GC_Space_Needed);
  207.   termination_suffix_trace (TERM_GC_OUT_OF_SPACE);
  208. }
  209.  
  210. void
  211. DEFUN_VOID (termination_eof)
  212. {
  213.   Microcode_Termination (TERM_EOF);
  214. }
  215.  
  216. void
  217. DEFUN (termination_signal, (signal_name), CONST char * signal_name)
  218. {
  219.   if (signal_name != 0)
  220.     {
  221.       termination_prefix (TERM_SIGNAL);
  222.       fprintf (stdout, "Killed by %s.\n", signal_name);
  223.     }
  224.   else
  225.     attempt_termination_backout (TERM_SIGNAL);
  226.   termination_suffix_trace (TERM_SIGNAL);
  227. }
  228.